package com.insurance.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.insurance.annotation.SystemLog;
import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.User;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.UserPageVo;
import com.insurance.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    private UserPageVo userPageVo;

    @PostMapping("/userInfo")
    public ResponseResult userInfo() {
        return userService.userInfo();
    }

    @PutMapping("/userInfo")
    @SystemLog(BusinessName = "更新用户信息接口")
    public ResponseResult updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/listAll")
    public ResponseResult listAll(){
        return ResponseResult.okResult(userService.list());
    }

    /**
     * 分页查询所有用户
     * @param
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody UserPageVo userPageVo){
        this.userPageVo = userPageVo;
        return userService.listByPage(userPageVo);
    }
    /**
     * 根据用户id查询用户信息
     */
    @GetMapping("/getById")
    public ResponseResult getById(Long id){
        User byId = userService.getById(id);
        if(ObjectUtils.isEmpty(byId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        return ResponseResult.okResult(byId);
    }
    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping()
    public ResponseResult add(@RequestBody User user){
        return userService.addUser(user);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id){
        if(userService.removeById(id)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
    }
}
